`
https://leetcode.cn/problems/continuous-subarray-sum/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var checkSubarraySum = function (nums, k) {
  const n = nums.length

  // 计算前缀和
  const prefix = new Array(n + 1)
  prefix[0] = 0
  for (let i = 1; i < prefix.length; i++) {
    prefix[i] = (prefix[i - 1] + nums[i - 1]) % k
  }

  // 哈希表存储前缀和对应的索引
  const valToIdx = new Map()
  for (let i = 0; i < prefix.length; i++) {
    if (!valToIdx.has(prefix[i])) {
      valToIdx.set(prefix[i], i)
    } else {
      // 如果出现子数组和为 k 的倍数且长度大于等于 2 时
      if (i - valToIdx.get(prefix[i]) >= 2) return true
    }
  }

  return false
};